package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.CuisineDao;
import com.cn.wanxi.model.CuisineModel;
import com.cn.wanxi.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CuisineImpl implements CuisineDao {
    @Override
    public boolean findByName(String name) {
        String sql = "select * from cuisine where name='" + name + "'";
        ResultSet resultSet = JDBC.getResultSet(sql);
        boolean isHave = false;
        try {
            while (resultSet.next()) {
                isHave = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return isHave;
    }

    @Override
    public int add(CuisineModel cuisineModel) {
        String sql = "insert into cuisine (name,is_show,create_time) values('" + cuisineModel.getName() + "'," + cuisineModel.getIsShow() + ",'" + cuisineModel.getCreateTime() + "')";
        return JDBC.excuteUpdate(sql);
    }

    @Override
    public List<CuisineModel> findAll() {
        String sql = "select * from cuisine";
        ResultSet resultSet = JDBC.getResultSet(sql);
        List<CuisineModel> list = new ArrayList<>();
        try {
            while (resultSet.next()) {
                CuisineModel model = new CuisineModel();
                model.setId(resultSet.getInt("id"));
                model.setName(resultSet.getString("name"));
                model.setCreateTime(resultSet.getDate("create_time").toString());
                model.setIsShow(resultSet.getInt("is_show"));
                list.add(model);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}
